package com.ruoyi.admin.controller;

import com.ruoyi.admin.service.IEventDetailService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EventDetail;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动录入Controller
 * 
 * @author xukai
 * @date 2024-12-16
 */
@RestController
@RequestMapping("/event/detail")
public class EventDetailController extends BaseController
{
    @Autowired
    private IEventDetailService eventDetailService;

    /**
     * 查询活动录入列表
     */
    //@PreAuthorize("@ss.hasPermi('event:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(EventDetail eventDetail)
    {
        startPage();
        List<EventDetail> list = eventDetailService.selectEventDetailList(eventDetail);
        //根据时间判断活动状态
        List<EventDetail> events = list.stream()
            .map(com.ruoyi.common.utils.EventStatusUtils::setEventStatus)
            .collect(Collectors.toList());
        return getDataTable(events);
    }

    /**
     * 导出活动录入列表
     */
    //@PreAuthorize("@ss.hasPermi('event:detail:export')")
    @Log(title = "活动录入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EventDetail eventDetail)
    {
        List<EventDetail> list = eventDetailService.selectEventDetailList(eventDetail);
        //根据时间判断活动状态
        List<EventDetail> events = list.stream()
            .map(com.ruoyi.common.utils.EventStatusUtils::setEventStatus)
            .collect(Collectors.toList());
        ExcelUtil<EventDetail> util = new ExcelUtil<EventDetail>(EventDetail.class);
        util.exportExcel(response, events, "活动录入数据");
    }

    /**
     * 获取活动录入详细信息
     */
    //@PreAuthorize("@ss.hasPermi('event:detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(eventDetailService.selectEventDetailById(id));
    }

    /**
     * 新增活动录入
     */
    //@PreAuthorize("@ss.hasPermi('event:detail:add')")
    @Log(title = "活动录入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EventDetail eventDetail)
    {
        return toAjax(eventDetailService.insertEventDetail(eventDetail));
    }

    /**
     * 修改活动录入
     */
    //@PreAuthorize("@ss.hasPermi('event:detail:edit')")
    @Log(title = "活动录入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EventDetail eventDetail)
    {
        return toAjax(eventDetailService.updateEventDetail(eventDetail));
    }

    /**
     * 删除活动录入
     */
    //@PreAuthorize("@ss.hasPermi('event:detail:remove')")
    @Log(title = "活动录入", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eventDetailService.deleteEventDetailByIds(ids));
    }

    /**
     * 查询字典列表
     * */
    @GetMapping("/eventDict")
    public AjaxResult eventDict(){
        return success(eventDetailService.eventDict());
    }

    /**
     * 改变活动展示状态
     * */
    @PutMapping("/changeShow")
    public AjaxResult changeShow(@RequestBody EventDetail eventDetail){
        return success(eventDetailService.changeShow(eventDetail));
    }
}
